package com.hnevc.servicedemo;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    //定义服务连接对象，指明了谁绑定到了那个服务
    MyServiceConnection conn = new MyServiceConnection();
       ProgressBar progressBar;
    //声明一个MyBinder的变量
    MyService.MyBinder myBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar=(ProgressBar)findViewById(R.id.id_progressbar);
        
     //TODO 注册广播接收者
    IntentFilter filter =new IntentFilter();
    filter.addAction("com.hnevc.servicedemo.PROGESS");
    MyBroadCastReceiver receiver=new MyBroadCastReceiver();

    registerReceiver(receiver, filter) ;

    }
    //将在startService按钮被点击的时候调用

    /**
     *
     * @param view 触发点击事件的视图对象
     */
    public void buttonClick(View view){

        //启动Service

        int id= view.getId();
        switch (id){
            case R.id.id_btn_start_service:{

                Intent intent = new Intent();
                intent.setClass(this,MyService.class);
                startService(intent);
                break;//结束分支
            }
            case R.id.id_btn_stop_service:{
                Intent intent = new Intent();
                intent.setClass(this,MyService.class);
                stopService(intent);
                break;//结束分支

            }
            case R.id.id_btn_bind_service:{

                Intent intent = new Intent();
                intent.setClass(this,MyService.class);
                //通过bind的方式启动服务，intent意图指定哪一个服务，para2：指代一个bind，
                // para3：如果bind的service原来没有创建，那么就创建他，否者直接bind
                bindService(intent, conn, BIND_AUTO_CREATE);
                break;
            }
            case R.id.id_btn_unbind_service:{
                unbindService(conn);
                break;
            }
            case R.id.id_btn_bind_play:{

                myBinder.call("PLAY");

                break;
            }
            case R.id.id_btn_bind_pause:{

                myBinder.call("PAUSE");

                break;
            }

            case R.id.id_btn_bind_stop:{

                myBinder.call("STOP");

                break;
            }



        }

    }
    class MyServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(
                ComponentName name
                , IBinder service) {

            myBinder = (MyService.MyBinder) service;

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            myBinder=null;
        }
    }
//定义广播的接收者类
    class MyBroadCastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {intent.getIntExtra("duration",0);

            int duration= intent.getIntExtra("duration", 0);
            int currentPosition = intent.getIntExtra("currentPosition", 0);
            Log.e(TAG,"onReceive:"+duration+"=="+currentPosition);
            progressBar.setMax(duration);
            progressBar.setProgress(currentPosition);
        }
    }
}
